#ifndef HMC5883L_H_
#define HMC5883L_H_

#include "C6747.h"

//Definition of HMC5883L Register
#define HMC5883_ADDR 0x1E
#define HMC5883_R_CONFA 0
#define HMC5883_R_CONFB 1
#define HMC5883_R_MODE 2
#define HMC5883_R_XM 3
#define HMC5883_R_XL 4
#define HMC5883_R_ZM 5
#define HMC5883_R_ZL 6
#define HMC5883_R_YM 7
#define HMC5883_R_YL 8
#define HMC5883_R_STATUS 9
#define HMC5883_R_IDA 10
#define HMC5883_R_IDB 11
#define HMC5883_R_IDC 12

#define HMC5883_CONTINMODE	0
#define HMC5883_SINGLEMODE	1
#define HMC5883_IDLEMODE	2

#define PI 3.1415926
#define DECLINATION 12.53

#define MAG_0P88GA 0x00
#define MAG_1P3GA 0x01
#define MAG_1P9GA 0x02
#define MAG_2P5GA 0x03
#define MAG_4P0GA 0x04
#define MAG_4P7GA 0x05
#define MAG_5P6GA 0x06
#define MAG_8P1GA 0x07


//
Uint8 HMC5883_RDREG(Uint8 regname);		//read&return the reg value
Uint8 HMC5883_WRREG(Uint8 regname, Uint8 value);	//write a byte to reg, and return if operation is success. 0--success, 1--fail.
void HMC5883_GETDATA(double *data, double mag_pitch, double mag_roll);		//Read magnetic field strength of XYZ from HMC5883
void HMC5883_SETMODE(Uint8 mode);		//set operating mode

void HMC5883_CONNECT(void);		//Connect to HMC5883L, read ID and identify
void HMC5883_INIT(void);
Uint8 HMC5883_SETRANGE(Uint8 range);
//Register construction
//HMC5883_R_CONFA:
//CRA7	MA1	MA0	DO2	DO1	DO0	MS1	MS0
//CRA7 -- should always be 0;
//MA1 MA0 -- select number of samples averaged(1 ~ 8) per measurement output
//				00 = 1, 01 = 2, 10 = 4, 11 = 8(Default)
//DO2 DO1 DO0 -- data output rate
//					000=0.75Hz, 001=1.5, 010=3, 011=7.5, 100=15(Default), 101=30, 110=75
//MS1 MS0 -- measurement mode
//				00=normal mode(default),01=positive bias,10=negative bias, 11=reserved

//HMC5883_R_CONFB:
//GN2 GN1 GN0 CRB4 CRB3 CRB2 CRB1 CRB0
//CRB4 CRB3 CRB2 CRB1 CRB0 -- should be 0;
//GN2 GN1 GN0 -- gain for device
//				000	0.88Ga	1370LSB/Ga	0xF800-0x07FF(-2048-2047)
//				001	1.3Ga	1090(default)
//				010	1.9Ga	820
//				011	2.5Ga	660
//				100	4.0Ga	440
//				101	4.7Ga	390
//				110	5.6Ga	330
//				111	8.1Ga	230

#endif /*HMC5883L_H_*/
